package com.xinfu.newproject.ui.pay_detail;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.WebPayActivity;
import com.xinfu.newproject.adapter.paycost_adapter.InvoiceAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.pay.bean.PayCostBean;
import com.xinfu.newproject.presenter.PayPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;
import com.xinfu.newproject.widget.SwitchButton;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class BillingDetailsFragment extends BaseActivity implements CompoundButton.OnCheckedChangeListener, IView.IPayPropertyView, InvoiceAdapter.EditTextLisener {

    @BindView(R.id.head_back)
    ImageView backIv;

    @BindView(R.id.layout_invoice_person_tv)
    TextView personTv;

    @BindView(R.id.layout_invoice_unit_tv)
    TextView unitTv;

    @BindView(R.id.fragment_billing_detail_title)
    TextView titleTv;

    @BindView(R.id.fragment_billing_detail_title_money)
    TextView titleMoneyTv;

    @BindView(R.id.fragment_billing_detail_include)
    LinearLayout invoiceLL;

    @BindView(R.id.fragment_billing_detail_sb)
    SwitchButton switchButton;

    @BindView(R.id.layout_invoice_rv)
    RecyclerView mrRecyclerView;

    private InvoiceAdapter invoiceAdapter;
    private boolean invoiceCheck = true;

    String[] editString1 = {"请填写开票人姓名(必填)", "请填写开票地址(必填)", "请填写收票人手机号", "请填写收票人邮箱"};
    String[] editString2 = {"请填写企业名称(必填)", "请填写企业税号(必填)", "请填写企业电话(必填)", "请填写企业地址(必填)", "请填写企业账号", "请填写企业邮箱"};

    private PayPresenter payPresenter = new PayPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_billing_detail);
        ButterKnife.bind(this);
        registerBack();
        initViewBind();
        initLisener();
    }

    private void initLisener() {
        switchButton.setOnCheckedChangeListener(this);
    }

    private void initViewBind() {
        setActivityTitle("账单支付");
        titleMoneyTv.setText("¥" + getIntent().getExtras().getDouble("payFee"));
        mrRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        invoiceAdapter = new InvoiceAdapter(this);
        mrRecyclerView.setAdapter(invoiceAdapter);
        invoiceAdapter.setNewData(Arrays.asList(editString1));
    }

    private int invoiceType = 0;

    @OnClick({R.id.layout_invoice_person_tv, R.id.layout_invoice_unit_tv, R.id.fragment_billing_detail_pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_invoice_person_tv:
                if (invoiceType == 1) {
                    personTv.setBackgroundResource(R.drawable.no_bg_text);
                    personTv.setTextColor(getResources().getColor(R.color.yellow_ff));
                    unitTv.setBackgroundResource(R.drawable.no_bg_text_normal);
                    unitTv.setTextColor(getResources().getColor(R.color.gray_a5));
                    invoiceAdapter.setNewData(Arrays.asList(editString1));
                    invoiceType = 0;
                    invoiceName = null;
                    invoiceAccount = null;
                    invoiceAddress = null;
                    invoiceMobile = null;
                    invoiceEmail = null;
                    invoiceEin = null;
                    invoiceAdapter.removeText(1);
                }
                break;
            case R.id.layout_invoice_unit_tv:
                if (invoiceType == 0) {
                    personTv.setBackgroundResource(R.drawable.no_bg_text_normal);
                    personTv.setTextColor(getResources().getColor(R.color.gray_a5));
                    unitTv.setBackgroundResource(R.drawable.no_bg_text);
                    unitTv.setTextColor(getResources().getColor(R.color.yellow_ff));
                    invoiceAdapter.setNewData(Arrays.asList(editString2));
                    invoiceType = 1;
                    invoiceName = null;
                    invoiceAccount = null;
                    invoiceAddress = null;
                    invoiceMobile = null;
                    invoiceEmail = null;
                    invoiceEin = null;
                    invoiceAdapter.removeText(1);
                }
                break;
            case R.id.fragment_billing_detail_pay:
                if (isLogin()) {
                    if (invoiceCheck) {
                        if (invoiceType == 0) {
                            if (invoiceName == null || invoiceAddress == null) {
                                if (invoiceName == null) {
                                    BToast.showText(this, "请填写开票人姓名");
                                } else if (invoiceAddress == null) {
                                    BToast.showText(this, "请填写开票人地址");
                                }

                            } else {
                                invoiceEin = null;
                                invoiceAccount = null;
                                if (invoiceEmail == null) {
                                    invoiceEmail = "";
                                }
                                if (invoiceMobile == null) {
                                    invoiceMobile = "";
                                }
                                payPresenter.payCost();
                            }
                        } else if (invoiceType == 1) {
                            if (invoiceName == null || invoiceMobile == null || invoiceAddress == null || invoiceEin == null) {
                                if (invoiceName == null) {
                                    BToast.showText(this, "请填写企业名称");
                                } else if (invoiceEin == null) {
                                    BToast.showText(this, "请填写企业税号");
                                } else if (invoiceMobile == null) {
                                    BToast.showText(this, "请填写企业电话");
                                } else if (invoiceAddress == null) {
                                    BToast.showText(this, "请填写企业地址");
                                }
                            } else {
                                if (invoiceEmail == null) {
                                    invoiceEmail = "";
                                }
                                if (invoiceAccount == null) {
                                    invoiceAccount = "";
                                }
                                payPresenter.payCost();
                            }
                        }
                    } else {
                        invoiceName = null;
                        invoiceAccount = null;
                        invoiceAddress = null;
                        invoiceMobile = null;
                        invoiceEmail = null;
                        invoiceEin = null;
                        payPresenter.payCost();
                    }

                }
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b) {
            invoiceLL.setVisibility(View.VISIBLE);
        } else {
            invoiceLL.setVisibility(View.INVISIBLE);
        }
        invoiceCheck = b;
    }

    /**
     * 判断是否可以请求数据
     */
    String userId, userName, userPhone;
    int urlType;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
//            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
            userPhone = resultBean.getUserInfo().getPhone();
        } else {
            BToast.showText(this, "请先登录");
            return false;
        }

        return true;
    }


    @Override
    public String header() {
        return getIntent().getExtras().getString("tenantId");
    }

    @Override
    public String billType() {
        return getIntent().getExtras().getString("billType");
    }

    @Override
    public String billids() {
        return getIntent().getExtras().getString("billids");
    }

    @Override
    public double payFee() {
        return getIntent().getExtras().getDouble("payFee");
    }

    @Override
    public String name() {
        return userName;
    }

    @Override
    public String mobile() {
        return userPhone;
    }

    @Override
    public String roomId() {
        return getIntent().getExtras().getString("roomId");
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String payType() {
        return "online";
    }

    @Override
    public String invoiceName() {
        return invoiceName;
    }

    @Override
    public String invoiceAddress() {
        return invoiceAddress;
    }

    @Override
    public String invoiceMobile() {
        return invoiceMobile;
    }

    @Override
    public String invoiceEmail() {
        return invoiceEmail;
    }

    @Override
    public String invoiceAccount() {
        return invoiceAccount;
    }

    @Override
    public String invoiceEin() {
        return invoiceEin;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(this, content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
        showLoding();
    }

    @Override
    public void hideProgress() {
        hideLoding();
    }

    @Override
    public void getMsg(PayCostBean payCostBean) {
        if (payCostBean.getResult() != null) {
            if (payCostBean.getResult().getPay_url() != null) {
                Bundle bundle = new Bundle();
                bundle.putString("response", payCostBean.getResult().getPay_url());
                startIntentActivity(WebPayActivity.class, bundle);
            }
        } else {
            BToast.showText(this, payCostBean.getMessage());
        }
    }

    String invoiceName, invoiceAddress, invoiceMobile, invoiceEmail, invoiceAccount, invoiceEin;

    @Override
    public void editLisener(int postion, String text) {
        if (invoiceType == 0) {
            switch (postion) {
                case 0:
                    invoiceName = text;
                    break;
                case 1:
                    invoiceAddress = text;
                    break;
                case 2:
                    invoiceMobile = text;
                    break;
                case 3:
                    invoiceEmail = text;
                    break;
            }
        } else if (invoiceType == 1) {
            switch (postion) {
                case 0:
                    invoiceName = text;
                    break;
                case 1:
                    invoiceEin = text;
                    break;
                case 2:
                    invoiceMobile = text;
                    break;
                case 3:
                    invoiceAddress = text;
                    break;
                case 4:
                    invoiceAccount = text;
                    break;
                case 5:
                    invoiceEmail = text;
                    break;
            }
        }
    }
}
